import { Inject, Controller, Get, Query, Post, Body } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
import { WxService } from '../service/wx.service';

@Controller('/open-api')
export class OpenController {
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Inject()
  wxService: WxService;

  @Post('/login')
  async login(
    @Body('account')
    account: string,
    @Body('password')
    password: string
  ) {
    return await this.userService.login(account, password);
  }

  @Get('/login')
  async get_login(
    @Query('account')
    account: string,
    @Query('password')
    password: string
  ) {
    return this.userService.login(account, password);
  }

  @Post('/login/openid')
  async loginByOpenid(
    @Body('openid')
    openid: string
  ) {
    return await this.userService.loginByOpenId(openid);
  }

  @Get('/user-check')
  async userCheck(
    @Query('login_code')
    login_code: string,
    @Query('phone_code')
    phone_code: string
  ) {
    return this.userService.userCheck(login_code, phone_code);
  }

  @Post('/user-check')
  async postUserCheck(
    @Body('login_code')
    login_code: string,
    @Body('phone_code')
    phone_code: string
  ) {
    return this.userService.userCheck(login_code, phone_code);
  }

  @Get('/get-jwt')
  async getJwt(@Query('token') token: string) {
    return this.userService.getJwt(token);
  }

  @Get('/set-jwt')
  async setJwt() {
    return this.userService.setJwt();
  }

  @Post('/user-openid')
  async getWxOpenid(
    @Body('js_code')
    js_code: string
  ) {
    return await this.wxService.wxAppLogin(js_code);
  }

  @Post('/user-phone')
  async getUserPhone(
    @Body('code')
    code: string
  ) {
    return await this.wxService.getUserPhoneNumber(code);
  }
}
